using HCore.IOC.Context.Event.Attr;
using HCore.Unity.Behaviour;
using HCore.Unity.Core;
using HCore.Unity.Extensions;
using JetBrains.Annotations;
using UI.UIGame.Menu;
using UnityEngine;

namespace Game.Background
{
    public class BackgroundVisual : HCoreMonoBehaviour
    {
        // 每块背景的大小 
        public int SideLength = 12800;

        private int mCurrentBgIndex;
        private Vector2Int mOffset = Vector2Int.zero;
        private Transform mTransform1;
        private Transform mTransform2;
        private Transform mTransform3;
        private Transform mTransform4;

        // Start is called before the first frame update
        private void Start()
        {
            mTransform1 = transform.Find("bg1");
            mTransform2 = transform.Find("bg2");
            mTransform3 = transform.Find("bg3");
            mTransform4 = transform.Find("bg4");

            RandomSwitchBackgroundImage();
            ResetSpriteRendererPosition(); // 初始触发一下
        }

        // Update is called once per frame
        private void Update()
        {
            var gap = 30;
            // gap = (1 / Time.deltaTime) switch
            // {
            //     > 90 => 60,
            //     > 70 => 45,
            //     > 50 => 30,
            //     > 20 => 15,
            //     _ => 10 
            // };
            if (Time.frameCount % gap == 0) return;
            var cameraPosition = GameUtils.MainCamera.transform.position;
            var symbol = (cameraPosition.x >= 0 ? 1 : -1, cameraPosition.y >= 0 ? 1 : -1);
            var offset = new Vector2Int(
                ((int)cameraPosition.x + SideLength / 2 * symbol.Item1) / SideLength,
                ((int)cameraPosition.y + SideLength / 2 * symbol.Item2) / SideLength
            );
            if (mOffset == offset) return;

            mOffset = offset;
            ResetSpriteRendererPosition();
        }

        #region EventListener

        [UsedImplicitly]
        [EventListener]
        public void TriggerNextSwitchBackgroundImageEvent(SwitchBackgroundCommand command)
        {
            mCurrentBgIndex++;
            if (mCurrentBgIndex > 6) mCurrentBgIndex = 0;
            SwitchBackgroundImage(mCurrentBgIndex);
        }

        #endregion


        public void RandomSwitchBackgroundImage()
        {
            mCurrentBgIndex = Random.Range(0, 7);
            SwitchBackgroundImage(mCurrentBgIndex);
        }

        private void SwitchBackgroundImage(int bgIndex)
        {
            var path = $"Images/Background/{bgIndex}";
            var sprite = Resources.Load<Sprite>(path);
            ConfigSpriteRenderer(mTransform1, sprite);
            ConfigSpriteRenderer(mTransform2, sprite);
            ConfigSpriteRenderer(mTransform3, sprite);
            ConfigSpriteRenderer(mTransform4, sprite);
        }

        /// <summary>
        ///     配置 SpriteRenderer
        /// </summary>
        /// <param name="trans"></param>
        /// <param name="sprite"></param>
        private void ConfigSpriteRenderer(Transform trans, Sprite sprite)
        {
            var spriteRenderer = trans.GetComponent<SpriteRenderer>();
            spriteRenderer.size = new Vector2(SideLength, SideLength);
            spriteRenderer.drawMode = SpriteDrawMode.Tiled;
            spriteRenderer.sprite = sprite;
        }

        private void ResetSpriteRendererPosition()
        {
            var offset = mOffset * SideLength;
            var off = SideLength / 2;

            mTransform1.position = (offset + new Vector2Int(-off, off)).ToVector3(mTransform1.position.z);
            mTransform2.position = (offset + new Vector2Int(off, off)).ToVector3(mTransform2.position.z);
            mTransform3.position = (offset + new Vector2Int(-off, -off)).ToVector3(mTransform3.position.z);
            mTransform4.position = (offset + new Vector2Int(off, -off)).ToVector3(mTransform4.position.z);
        }
    }
}